<?php
/**
*所有的市场数据
*/
require("../include/init.php");
// $mysql=mysql::getIns();
date_default_timezone_set('Asia/Shanghai');
$mysqli=mysql::getIns();


/*******************************************************************************

                            时间条件

*******************************************************************************/
$maxtime=time();
$mintime=$maxtime-10*60;
//统计当天累计人数数
$t = time();
$start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
$end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
//昨天时间
$startsec = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t))-24*3600;
$endsec = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t))-24*3600;

//统计本周
/****************
*获取本周开始时间和结束时间
*
***********/

$date=date('Y-m-d');  //当前日期
$first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
$now_end=date('Y-m-d',strtotime("$now_start +6 days"));  //本周结束日期
$one=strtotime($now_start);
$two=$one+24*3600*1;
$three=$one+24*3600*2;
$four=$one+24*3600*3;
$five=$one+24*3600*4;
$six=$one+24*3600*5;
$seven=$one+24*3600*6;
$nowone=$one+24*3600*7;

/**************************
获取本月第一个周一的时间

**********************/
$year=date('Y',time());
$month=date('m',time());
$base_time = strtotime ( "{$year}-{$month}-01" );
$base_w = date ( "w", $base_time );

if($base_w == '1'){
  //刚好是星期一
  $first = $base_time;
}else{
  //不是星期一
  //找到上个星期天是几月几号
  $last_sun = date ( "Y-m-d", strtotime("-$base_w day", $base_time));
  //在上个星期天的基础上 加8天
  $first = strtotime ( "+8 day", strtotime ( $last_sun ) );
}
//第二个周一的时间
$second=$first+7*24*3600;
// echo date('Y-m-d H:i:s',$second);
// exit;
//第三个时间戳
$third=$second+7*24*3600;
// echo date('Y-m-d H:i:s',$third);
// exit;
//第四个时间戳
$fourth=$third+7*24*3600;
// echo date('Y-m-d H:i:s',$fourth);
// exit;
//下周一时间戳
$fifth=$fourth+7*24*3600;
// echo date('Y-m-d H:i:s',$third);
// exit;


//获取第一季度开始时间戳
$firstmonth =mktime(0,0,0,1,1,date('Y',time()));

//获取第二季度开始时间
$secondmonth =mktime(0,0,0,4,1,date('Y',time()));
// echo date('Y-m-d H:i:s',$secondmonth);

//获取第三季度开始时间
$thirdmonth =mktime(0,0,0,7,1,date('Y',time()));
// echo date('Y-m-d H:i:s',$thirdmonth);

//获取第四季度开始时间
$fourthmonth =mktime(0,0,0,10,1,date('Y',time()));
// echo date('Y-m-d H:i:s',$fourthmonth);

//获取明年第一季度开始时间
$firstmonths =mktime(0,0,0,1,1,date('Y',time())+1);
// echo date('Y-m-d H:i:s',$firstmonths);


/*******************************************************************************

                            sql条件

*******************************************************************************/



//现在人
$sql="select count(*) from alldev where whitelist <> '1' and isOL=1 and timestamp > $mintime and timestamp < $maxtime and shop <> 'Audi City BeiJing';";
// $allnow=$mysql->getOne($sql);
//所有人
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop <> 'Audi City BeiJing';";
// $allday=$mysql->getOne($sql);
//昨天人
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp > $startsec and timestamp < $endsec and shop <> 'Audi City BeiJing';";
// $yesdallday=$mysql->getOne($sql);


//统计本周周一进店量
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $one and timestamp < $two and shop <> 'Audi City BeiJing';";
// $onejk=$mysql->getOne($sql);
//统计本周周二进店量
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $two and timestamp < $three and shop <> 'Audi City BeiJing';";
// $twojk=$mysql->getOne($sql);
//统计本周周三进店量
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $three and timestamp < $four and shop <> 'Audi City BeiJing';";
// $threejk=$mysql->getOne($sql);
//统计本周周四进店量
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $four and timestamp < $five and shop <> 'Audi City BeiJing';";
// $fourjk=$mysql->getOne($sql);
//统计本周周五进店量
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $five and timestamp < $six and shop <> 'Audi City BeiJing';";
// $fivejk=$mysql->getOne($sql);
// echo $fivejk;
// exit;
//统计本周周六进店量
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $six and timestamp < $seven and shop <> 'Audi City BeiJing';";
// $sql="select * from alldev where whitelist <> '1' and timestamp >= $six and timestamp < $seven and shop='". $address ."'";
// $sixjk=$mysql->getOne($sql);
// echo $sixjk;
// exit;
//统计本周周日进店量
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $seven and timestamp < $nowone and shop <> 'Audi City BeiJing';";
// $sevenjk=$mysql->getOne($sql);


//统计本月人数
//获取这个月第一周的人数
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $first and timestamp < $second and shop <> 'Audi City BeiJing';";
// $firstjk=$mysql->getOne($sql);
// echo $firstjk;
// exit;

//获取这个月第二周的人数
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $second and timestamp < $third and shop <> 'Audi City BeiJing';";
// $secondjk=$mysql->getOne($sql);
// $bak=$secondjk;
// echo $secondjk;
// exit;

//获取这个月第三周的人数
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $third and timestamp < $fourth and shop <> 'Audi City BeiJing';";
// $thirdjk=$mysql->getOne($sql);
// echo $thirdjk;
// exit;

//获取这个月第四周的人数
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $fourth and timestamp < $fifth and shop <> 'Audi City BeiJing';";
// $fourthjk=$mysql->getOne($sql);


//统计今年

//获取第一季度的人数
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $firstmonth and timestamp < $secondmonth and shop <> 'Audi City BeiJing';";
// $firstmonthjk=$mysql->getOne($sql);
// echo $firstmonth;
// exit;

//获取第二季度的人数
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $secondmonth and timestamp < $thirdmonth and shop <> 'Audi City BeiJing';";
// $secondmonthjk=$mysql->getOne($sql);

//获取第三季度的人数
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $thirdmonth and timestamp < $fourthmonth and shop <> 'Audi City BeiJing';";
// $thirdmonthjk=$mysql->getOne($sql);
// echo $thirdmonthjk;
// exit;


//获取第四季度的人数
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp >= $fourthmonth and timestamp < $firstmonths and shop <> 'Audi City BeiJing';";
// $fourthmonthjk=$mysql->getOne($sql);


//统计当天累计人数数

//统计进店频次为2次的
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end  and visittimes='2' and shop <> 'Audi City BeiJing';";
// $visittwo=$mysql->getOne($sql);
// echo $visittwo;
// exit;

//统计进店频次为3次
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end  and visittimes='3' and shop <> 'Audi City BeiJing';";
// $visitthree=$mysql->getOne($sql);
// echo $visitthree;
// exit;



//统计进店频次为3次以上
$sql .="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end  and visittimes > '3' and shop <> 'Audi City BeiJing'";
// $visitmore=$mysql->getOne($sql);

$data=$mysqli->getDuo($sql);
// print_r($data);
// exit;

/*******************************************************************************

                                      执行完计算

*******************************************************************************/

$allnow=$data[0];
$allday=$data[1];
$yesdallday=$data[2];
$onejk=$data[3];
$twojk=$data[4];
$threejk=$data[5];
$fourjk=$data[6];
$fivejk=$data[7];
$sixjk=$data[8];
$sevenjk=$data[9];
$firstjk=$data[10];
$secondjk=$data[11];
$thirdjk=$data[12];
$fourthjk=$data[13];
$firstmonthjk=$data[14];
$secondmonthjk=$data[15];
$thirdmonthjk=$data[16];
$fourthmonthjk=$data[17];
$visittwo=$data[18];
$visitthree=$data[19];
$visitmore=$data[20];



/*************************************

	进店频次(每天统计一次,今天累计人数的进店频次)


************************************/

//计算频次

//两次
$vtwo=floor(($visittwo/$allday)*100);
// echo $vtwo;
// exit;
//三次
$vthree=floor(($visitthree/$allday)*100);
// echo $vthree;
// exit;

//三次以上
$vmore=floor(($visitmore/$allday)*100);
// echo $vmore;
// exit;

//一次
$vone=100-$vtwo-$vthree-$vmore;
// echo $vone;
// exit;


$redis = new Redis();
  //连接服务器
  $redis->connect("localhost");
  //授权
  $redis->auth("redis=pwd@2016");
$redis->hmset('National_MarketDirector', array(
'allnow'=>$data[0],
'allday'=>$data[1],
'yesdallday'=>$data[2],
'onejk'=>$data[3],
'twojk'=>$data[4],
'threejk'=>$data[5],
'fourjk'=>$data[6],
'fivejk'=>$data[7],
'sixjk'=>$data[8],
'sevenjk'=>$data[9],
'firstjk'=>$data[10],
'secondjk'=>$data[11],
'thirdjk'=>$data[12],
'fourthjk'=>$data[13],
'firstmonthjk'=>$data[14],
'secondmonthjk'=>$data[15],
'thirdmonthjk'=>$data[16],
'fourthmonthjk'=>$data[17],
'visittwo'=>$data[18],
'visitthree'=>$data[19],
'visitmore'=>$data[20],
'vthree'=>$vthree,
'vmore'=>$vmore,
'vtwo'=>$vtwo,
'vone'=>$vone));
